public class Printer {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
        // write code here
        if(mat.length == 0) return new int[0];
        int l = 0, r = m - 1, t = 0, d = n - 1, x = 0;
        int[] ans = new int[(r + 1) * (d + 1)];
        while (true) {
            for(int i = l; i <= r; i++) ans[x++] = mat[t][i];
            if(++t > d) break;
            for(int i = t; i <= d; i++) ans[x++] = mat[i][r];
            if(--r < l) break;
            for(int i = r; i >= l; i--) ans[x++] = mat[d][i];
            if(--d < t) break;
            for(int i = d; i >= t; i--) ans[x++] = mat[i][l];
            if(++l > r) break;
        }
        return ans;
    }
}
